﻿using OnlineMealOrdering.Domain;
using OnlineMealOrdering.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OnlineMealOrdering.Common;

namespace OnlineMealOrdering.Areas.WebSiteManage.Models
{
    public static class EntityToModel
    {
        public static AccountModel ToAccountModel(this UserEntity entity)
        {
            var model = new AccountModel()
            {
                Id = entity.Id,
                UserCode = entity.UserCode,
                Name = entity.Name
            };
            model.FaceImage = entity.FaceImage == null ? "" : entity.FaceImage;
            model.Roles = new List<string>();
            model.Latitude = entity.Latitude;
            model.Longitude = entity.Latitude;
            model.CreatedDate = entity.CreateDate;
            model.AccountType = EnumHelper.GetEnumDescription(entity.AccountType);
            model.UserStatus = EnumHelper.GetEnumDescription(entity.Status);
            entity.Roles.ToList().ForEach(a => model.Roles.Add(a.RoleCode));
            return model;
        }

        public static StoreOnlineModel ToStoreOnlineModel(this StoreOnlineEntity entity)
        {
            var model = new StoreOnlineModel();
            model.Id = entity.Id;
            model.Name = entity.Name;
            model.Latitude = entity.Latitude;
            model.Longitude = entity.Longitude;
            model.LogoImage = entity.LogoImage.ToImageModel();
            if (entity.Images != null)
            {
                entity.Images.ToList().ForEach(a =>
                {
                    model.Images.Add(a.ToImageModel());
                });
            }
            model.InitiatePrice = entity.InitiatePrice;
            model.Phone = entity.Phone;
            model.QQ = entity.QQ;
            model.SortNo = entity.SortNo;
            model.StrStatus = EnumHelper.GetEnumDescription(entity.Status);
            model.StrType = EnumHelper.GetEnumDescription(entity.Type);
            model.Tel = entity.Tel;
            model.WeiBo = entity.WeiBo;
            if (entity.AreaCoordinate != null)
            {
                model.AreaCoordinate = new List<AreaCoordinateModel>();
                entity.AreaCoordinate.ToList().ForEach(a =>
                {
                    model.AreaCoordinate.Add(new AreaCoordinateModel() { Latitude = a.Latitude, Longitude = a.Longitude });
                });
            }
            if (entity.Marks != null)
            {
                model.Marks = new List<MarkModel>();
                entity.Marks.ToList().ForEach(a =>
                {
                    model.Marks.Add(a.ToMarkModel());
                });
            }
            if (entity.Tags != null)
            {
                model.Tags = new List<TagModel>();
                entity.Tags.ToList().ForEach(a =>
                {
                    model.Tags.Add(a.ToTagMdoel());
                });
            }

            return model;
        }

        public static MarkModel ToMarkModel(this MarkEntity entity)
        {
            return new MarkModel()
            {
                Description = entity.Description,
                Id = entity.Id,
                Image = entity.Image == null ? "" : entity.Image.Path,
                Name = entity.Name
            };
        }

        public static TagModel ToTagMdoel(this TagEntity entity)
        {
            return new TagModel()
            {
                Image = entity.Image == null ? "" : entity.Image.Path,
                Key = entity.Key

            };
        }

        public static ImageModel ToImageModel(this ImageEntity entity)
        {
            if (entity == null) return new ImageModel();
            return new ImageModel()
            {
                Name = entity.Name,
                Path = entity.Path,
                ThumbnailName = entity.ThumbnailName,
                ThumbnailPath = entity.ThumbnailPath
            };
        }
    }
}